/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package frontController;

import businessDelegates.FormularioBusinessDelegate;
import businessDelegates.PaisBusinessDelegate;
import businessObjects.Formulario;
import businessObjects.Pais;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author rene
 */
@WebServlet(name="contactoFrontController", urlPatterns={"/contactoFrontController"})
public class contactoFrontController extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       
        
        String action = request.getParameter("action");
        HttpSession currentSession = request.getSession();
        //HttpSession currentSession = request.getSession();
        /*if(currentSession == null){
            response.sendRedirect("index.jsp");
        }else{*/
            if(action==null){
                currentSession.setAttribute("error", "Ha ocurrido un error, por favor intente nuevamente.");
                response.sendRedirect("error.jsp");
            }
            if(action.equals("tocontactform")){
                List<Pais> lp = null;
                PaisBusinessDelegate pbd = new PaisBusinessDelegate();
                try{
                    lp = pbd.todosLosPaises();
                    currentSession.setAttribute("listapaises", lp);
                    int idp = Integer.parseInt(request.getParameter("idPauta"));
                    currentSession.setAttribute("idpauta", idp);
                    response.sendRedirect("registro.jsp");
                }catch(Exception e){
                    e.printStackTrace();
                    response.sendRedirect("index.jsp");
                }

            }     
        
          if(action.equals("contactNew")){
                Formulario f = new Formulario();
                Pais p = new Pais();
                PaisBusinessDelegate pbd = new PaisBusinessDelegate();
                p = pbd.buscarPaisPorNombre(request.getParameter("pais"));
                try{
                    f.setDireccion(request.getParameter("direccion"));
                    f.setTelefono(request.getParameter("telefono"));
                    f.setEmail(request.getParameter("email"));
                    f.setNombre(request.getParameter("nombre"));
                    f.setPais(p);
                    f.setCiudad(request.getParameter("ciudad"));
                }catch(Exception e){
                    currentSession.setAttribute("message", "Ha ocurrido un error, por favor intente nuevamente");
                    response.sendRedirect("contacto.jsp");
                }
                try{
                    FormularioBusinessDelegate fbd = new FormularioBusinessDelegate();
                    fbd.guardarFormulario(f);
                    currentSession.setAttribute("message", "Formulario de contacto enviado");
                }catch(Exception e){
                    currentSession.setAttribute("message", "Error al guardar el formulario");
                }
                response.sendRedirect("contacto.jsp");
            }
        if(action.equals("viewContactInfo")){
            if(currentSession == null || !currentSession.getAttribute("rol").equals("cliente")){
                //request.getRequestDispatcher("index.jsp").forward(request,response);
                response.sendRedirect("index.jsp");
                }else{
                    int idPauta = Integer.parseInt(request.getParameter("idPauta"));
                    List<Formulario> lf;
                    FormularioBusinessDelegate fbd = new FormularioBusinessDelegate();
                    lf = fbd.consultarFormulariosPauta(idPauta);
                    currentSession.setAttribute("listaForms", lf);
                    response.sendRedirect("cliente/misformularios.jsp");
                }
        }
        //}

      //  response.setContentType("text/html;charset=UTF-8");
      //  PrintWriter out = response.getWriter();
       // try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet contactoFrontController</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet contactoFrontController at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
     //   } finally {
     //       out.close();
       // }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
